package com.hn.naixue;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

import java.io.IOException;
import java.net.URI;

/**
 * @Author: Qingwr
 * @Date: 2022/8/11 21:14
 * @Description 不同方式获取FileSystem实例
 */
public class FileSystemFactory {

    //HDFS地址
    private static final String FS = "hdfs://hadoop0:8020";
    //远程用户
    /*默认用当前用户进行操作,需要指定！*/
    private static String FS_USER = "root";

    public static FileSystem getFileSystem1() throws IOException {
        //1:创建Configuration对象
        Configuration conf = new Configuration();
        //2:设置文件系统类型
        conf.set("fs.defaultFS", FS);
        //3:设置用户
        //3:获取指定文件系统
        return FileSystem.get(conf);
    }

    public static FileSystem getFileSystem2() throws IOException {
        //1:创建Configuration对象
        Configuration conf = new Configuration();
        //2:设置文件系统类型
        conf.set("fs.defaultFS", FS);
        //3:获取指定文件系统
        return FileSystem.newInstance(conf);
    }

    public static FileSystem getFileSystem3() throws Exception {
        return FileSystem.get(new URI(FS), new Configuration(),FS_USER);
    }

    public static FileSystem getFileSystem4() throws Exception {
        return FileSystem.newInstance(new URI(FS), new Configuration(),FS_USER);
    }
}
